from django import forms
from apps.BaseForm import FormMixin
from apps.app_talk.models import Talk, Mood


class TalkForm(forms.ModelForm, FormMixin):
    mood = forms.IntegerField()

    class Meta:
        model = Talk
        fields = ('content', 'mood')

    def clean_mood(self):
        mood_id = self.cleaned_data.get('mood')
        if not mood_id:
            raise forms.ValidationError('请选择一个心情分类~')
        try:
            mood = Mood.objects.get(id=mood_id)
        except Mood.DoesNotExist:
            raise forms.ValidationError('该心情不存在~~')
        return mood
